package com.example.idachuappone.index.item;

import java.util.List;

import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.MainTWo;
import com.example.idachuappone.R;
import com.example.idachuappone.cook.action.CookAction;
import com.example.idachuappone.cook.entity.City;
import com.example.idachuappone.cook.entity.Set;
import com.example.idachuappone.index.ui.MyListView;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.CusProSmallBgDialog;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class SearchCityActivity extends BaseActivity implements OnItemClickListener, OnClickListener {
	private Button btn_back;
	private String cityname;
	private String citynameDingwei;
	private CookAction cookAction;
	private CusProSmallBgDialog cusProDialog;
	private Dialog dialogLoading;
	private int index;
	private MyListView mlv_main;
	private MyAdapter myAdapter;
	private Set set;
	private TextView tv_city_name;

	private class MyAdapter extends BaseAdapter {
		private Context context;
		private int index;
		private List<City> list;

		public void setIndex(int index) {
			this.index = index;
		}

		public void setList(List<City> list) {
			this.list = list;
		}

		public MyAdapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			return this.list == null ? 0 : this.list.size();
		}

		@Override
		public Object getItem(int position) {
			return this.list == null ? null : (City) this.list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LinearLayout.inflate(this.context, R.layout.simple_search_city_item, null);
				holder.img_city_p = (ImageView) convertView.findViewById(R.id.img_city_p);
				holder.tv_city_name = (TextView) convertView.findViewById(R.id.tv_city_name);
				holder.tv_xian = (TextView) convertView.findViewById(R.id.tv_xian);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			City city = this.list.get(position);
			if (position == this.index) {
				holder.img_city_p.setVisibility(0);
			} else {
				holder.img_city_p.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			}
			if (position == this.list.size() - 1) {
				holder.tv_xian.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			} else {
				holder.tv_xian.setVisibility(0);
			}
			holder.tv_city_name.setText(city.getName());
			return convertView;
		}
	}

	private class ViewHolder {
		ImageView img_city_p;
		TextView tv_city_name;
		TextView tv_xian;

		private ViewHolder() {
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_city);
		this.cookAction = new CookAction();
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.cusProDialog = new CusProSmallBgDialog(this);
		this.cityname = getIntent().getStringExtra("cityname");
		this.citynameDingwei = getIntent().getStringExtra("citynameDingwei");
		initView();
		initData();
	}

	private void initData() {
		this.dialogLoading.show();
		NetUtil.get(this, Constant.SETS, 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				SearchCityActivity.this.dialogLoading.dismiss();
				try {
					JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
					if (jsonObject.getInt("code") == 10000) {
						SearchCityActivity.this.set = new Set();
						SearchCityActivity.this.set.parseJson(jsonObject.getJSONObject("data"));
						SearchCityActivity.this.index = SearchCityActivity.this.cookAction.getCityIndex(
								SearchCityActivity.this.cityname, SearchCityActivity.this.set.getCity());
						SearchCityActivity.this.initAdapter(SearchCityActivity.this.index,
								SearchCityActivity.this.set.getCity());
						return;
					}
					MainToast.show(SearchCityActivity.this, jsonObject.getString("error"), 0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				SearchCityActivity.this.dialogLoading.dismiss();
				MainToast.show(SearchCityActivity.this,
						SearchCityActivity.this.getResources().getString(R.string.netFail), 0);
			}
		});
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.tv_city_name = (TextView) findViewById(R.id.tv_city_name);
		this.tv_city_name.setOnClickListener(this);
		this.tv_city_name.setText(this.citynameDingwei);
		this.mlv_main = (MyListView) findViewById(R.id.mlv_main);
		this.myAdapter = new MyAdapter(this);
		this.mlv_main.setAdapter(this.myAdapter);
		this.mlv_main.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.tv_city_name /* 2131165405 */:
			if (this.citynameDingwei != null
					&& !this.citynameDingwei.equals("\u65e0\u6cd5\u5b9a\u4f4d\u5f53\u524d\u57ce\u5e02")
					&& this.set != null && this.set.getCity() != null && this.set.getCity().size() > 0) {
				this.index = this.cookAction.getCityIndex(this.citynameDingwei, this.set.getCity());
				AppShareData.isRefreshCityCook = true;
				AppShareData.isRefreshCityIndex = true;
				AppShareData.city_id = this.set.getCity().get(this.index).getId();
				AppShareData.cityName = this.set.getCity().get(this.index).getName();
				startActivity(new Intent(this, MainTWo.class));
			}
		default:
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		AppShareData.isRefreshCityCook = true;
		AppShareData.isRefreshCityIndex = true;
		AppShareData.city_id = this.set.getCity().get(position).getId();
		AppShareData.cityName = this.set.getCity().get(position).getName();
		startActivity(new Intent(this, MainTWo.class));
	}

	private void initAdapter(int index, List<City> list) {
		this.myAdapter.setIndex(index);
		this.myAdapter.setList(list);
		this.myAdapter.notifyDataSetChanged();
	}
}
